import SparkMD5 from 'spark-md5';

// 将文件分片
export const createChunk = (file, index, chunkSize) => {
  return new Promise((resolve) => {
    const start = index * chunkSize;
    const end = start + chunkSize;
    const blob = file.slice(start, end);

    const spark = new SparkMD5.ArrayBuffer();
    const fileReader = new FileReader();
    fileReader.onload = (e: any) => {
      spark.append(e.target.result); // 将每个切片的数据添加到 MD5 计算中
      resolve({
        start,
        end,
        index,
        hash: spark.end(),
        blob,
      }); // 当所有切片读取完成后返回 MD5 哈希值
    };

    fileReader.readAsArrayBuffer(blob);
  });
};
